import 'package:flutter/material.dart';
import 'package:jvtd_flutter_utils/src/text/text_util.dart';
import 'package:oktoast/oktoast.dart';

/// Toast
class ToastUtil {
  static int _lastToastTime;
  static String _lastToastText;

  /// 显示提示
  ///
  /// [msg] 提示信息
  /// [duration] 显示时长
  /// [position] 位置
  /// [backgroundColor] 背景颜色
  /// [textColor] 文本颜色
  /// [fontSize] 字体大小
  static void show(
    String msg, {
    int duration = 2000,
    ToastPosition position,
    Color backgroundColor,
    Color textColor,
    double fontSize,
  }) {
    if (TextUtil.isEmpty(msg)) return;

    // 如果是重复文案，且旧的toast并未消失，那么，不再显示新的Toast
    int now = DateTime.now().microsecondsSinceEpoch;
    if (_lastToastText == msg && _lastToastTime != null && now - _lastToastTime < duration) return;

    _lastToastText = msg;
    _lastToastTime = now;

    showToast(
      msg,
      duration: Duration(milliseconds: duration),
      dismissOtherToast: true,
      position: position,
      textStyle: TextStyle(fontSize: fontSize, color: textColor),
      backgroundColor: backgroundColor,
    );
  }

  /// 显示提示控件
  ///
  /// [widget] 提示控件
  /// [duration] 显示时长
  /// [position] 位置
  static void showWidget(
    Widget widget, {
    int duration = 2000,
    ToastPosition position,
  }) {
    if (widget == null) return;
    showToastWidget(
      widget,
      duration: Duration(milliseconds: duration),
      dismissOtherToast: true,
      position: position,
    );
  }

  /// 取消显示
  static void cancelToast() {
    dismissAllToast();
  }
}
